package com.bw.gmall.realtime.common.Util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


@Slf4j
public class HbaseUtil {

//    获取hbase连接

    public static Connection getHbaseConnection() throws IOException {


        return ConnectionFactory.createConnection();
    }



//    关闭连接

    public static void  closeConnection(Connection conn) throws IOException {

        if(conn != null){
            conn.close();
        }

    }

//    删除表
    public static void dropHbaseTable(Connection hbaseconn,String nameSpace,String table) throws IOException {

//        获取一个admin
        Admin admin = hbaseconn.getAdmin();

        TableName tableName = TableName.valueOf(nameSpace, table);

        if(admin.tableExists(tableName)){
//            先禁用
            admin.disableTable(tableName);
//            再删除
            admin.deleteTable(tableName);

        }

//        关闭
        admin.close();
        log.info(nameSpace + " " + table + "删除成功");

    }


//    创建表
    public static void createHbaseTable(Connection hbaseconn,String nameSpace,String table,String ...families) throws IOException {

        Admin admin = hbaseconn.getAdmin();

        TableName tableName = TableName.valueOf(nameSpace, table);

//        判断创建的表是否存在
            if(admin.tableExists(tableName)){
                return;
            }

//            创建表格描述器
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(nameSpace, table));

//            如果有多个列簇

        for (String family : families) {

            ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).build();

            tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);

        }

//        使用admin创建表格

        try {
            admin.createTable(tableDescriptorBuilder.build());
        }catch (Exception e){
            System.out.println("e = " + e );
            System.out.println("当前表格已存在 不需要创建 " + nameSpace + " : " + table );
        }

        admin.close();
        log.info(nameSpace + ":" + table + " 建表成功");


    }

//    增加数据

//     连接器  命名空间  表名  行键 列簇  数据
    public static void putDatas(Connection conn, String nameSpace, String tableName, String rowkey, String family, JSONObject data) throws IOException {

//        获取table
        Table table = conn.getTable(TableName.valueOf(nameSpace, tableName));

//        创建写入对象
        Put put = new Put(Bytes.toBytes(rowkey));

        for (String column : data.keySet()) {
            String columnvalue = data.getString(column);
            if(columnvalue != null){
                put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column),Bytes.toBytes(columnvalue));
            }
        }
//        调用方法写入数据
        try {
            table.put(put);
        }catch (Exception e){
            e.printStackTrace();
        }
//        关闭table
        table.close();


    }
//    删除数据

    public static void deleteDatas(Connection conn, String nameSpace, String tableName, String rowkey) throws IOException {

//        获取table
        Table table = conn.getTable(TableName.valueOf(nameSpace,tableName));
//        创建删除对象
        Delete delete = new Delete(Bytes.toBytes(rowkey));
//        调用方法删除表
        try {
            table.delete(delete);
        }catch (Exception e){
            e.printStackTrace();
        }
//        关闭table
        table.close();

    }

}
